<?php
/**
 * 【用户】注册接口
 * @author webopenfather
 */

// 导入公共库文件
include '../../util/function.php';
include '../../util/db.php';

// 1. 接受数据（用户名和密码）
$uname = @$_REQUEST['uname'];
$pwd = @$_REQUEST['pwd'];
$captcha = @$_REQUEST['captcha'];

if (!$uname || !$pwd || !$captcha) response(400, '参数有误');
session_start();
if (!@$_SESSION['captcha']) response(400, '验证码有误');
if ($_SESSION['captcha'] != $captcha) response(400, '验证码有误');

// 2. 操作数据库
    //2.1 检测用户是否存在
    $pdoStatement = $pdo->query("select * from ecs_users where username = '{$uname}'");
    $isFind = $pdoStatement->fetch(PDO::FETCH_ASSOC);
    if ($isFind) response(400, '用户已存在');
    //2.2 密码过滤
    $pwd = md5($pwd);
    //2.3 插入
    $rs = $pdo->exec("insert into ecs_users (username,password) values ('{$uname}','{$pwd}') ");
    // 2.4 判断
    if (!$rs) response(500, '注册失败');
    
// 3. 响应结果
response(201, '注册成功');

